package cn.singlewindow.ha.util.collector.collecter.service.impl;

import cn.singlewindow.ha.util.collector.collecter.constant.DataTableConstant;
import cn.singlewindow.ha.util.collector.collecter.domain.*;
import cn.singlewindow.ha.util.collector.collecter.mapper.*;
import cn.singlewindow.ha.util.collector.collecter.service.IParserService;
import cn.singlewindow.ha.util.collector.config.AppParameters;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Service
public class ParserService implements IParserService {

    private final static Log logger = LogFactory.getLog(ParserService.class);

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;
    @Autowired
    private CompanyNatureMapper companyNatureMapper;
    @Autowired
    private CountryAreaMapper countryAreaMapper;
    @Autowired
    private TradeModeMapper tradeModeMapper;
    @Autowired
    private AppParameters appParameters;

    @Autowired
    private CommonDomainMapper commonDomainMapper;

    /**
     * 1 获取文件名，有：匹配后缀，无，不处理
     * 2 后缀html,做html处理table，xlsx做excel处理
     * 3 html获取
     * @param message
     */
    @Override
    public void messageParser(Message message) {
    }

    @Override
    public void messageParser(byte[] bytes) {
    }

    @Override
    public boolean htmlParser(String payload) {
        try {

            Document document = Jsoup.parse(payload);
            // 获取标题
            Elements body = document.getElementsByTag("body");
            String bodyHtml=body.get(0).html();
            String title = bodyHtml.substring(0,bodyHtml.indexOf("<br>")).trim();

            String dataType = DataTableConstant.OTHER_TYPE;
            if(title.contains(DataTableConstant.GOODS_CATEGORY)) {
                dataType = DataTableConstant.GOODS_CATEGORY;
            }else if(title.contains(DataTableConstant.COMPANY_NATURE)){
                dataType = DataTableConstant.COMPANY_NATURE;
            }else if(title.contains(DataTableConstant.COUNTRY_AREA)){
                dataType = DataTableConstant.COUNTRY_AREA;
            }else if(title.contains(DataTableConstant.TRADE_MODE)){
                dataType = DataTableConstant.TRADE_MODE;
            }
            logger.info(String.format("开始处理类型[%s]，原文件名：[%s]",dataType,title));
            System.out.println(String.format("开始处理类型[%s]，原文件名：[%s]",dataType,title));
            if(DataTableConstant.OTHER_TYPE.equals(dataType)){
//                return false;
            }

            // 获取表格
            Elements tableRows = document.getElementsByTag("table").get(0).select("tr");
            // 配置固定数据
            String proviceName = "河南省";
            String ieType ="";
            String yearValue = "";
            int monthValue = 0;
            String yearMonthValue = "";
//            yearValue = title.substring(0,title.indexOf("年"));
//            monthValue = title.substring(title.indexOf("年")+1,title.indexOf("月")).contains("至")?Integer.valueOf(title.substring(title.indexOf("至")+1,title.indexOf("月"))):Integer.valueOf(title.substring(title.indexOf("年")+1,title.indexOf("月")));
//            yearMonthValue = yearValue+String.format("%02d",monthValue);
            yearMonthValue = getYearMonthFromTitle(title);
            proviceName = title.contains("省")?title.substring(title.indexOf("月")+1,title.indexOf("省")+1):"";
            if(!title.contains("进出口")){
                if(title.indexOf("进口")!=-1){
                    ieType = "I";
                } else if(title.indexOf("出口")!=-1){
                    ieType = "E";
                }
            }
            handle_zs(payload);
//            if(!StringUtils.hasLength(proviceName)){
//                return true;
//            } else {
//                handle_zz(payload);
//            }
//            return false;

//            if(tableRows.size()==1){
//                System.out.println("没有数据");
//                logger.error("本次处理表格中没有数据");
//            } else {
//                if(dataType.equals(DataTableConstant.GOODS_CATEGORY)){
//                    String text = "";
//                    String first_class = "";
//                    String second_class = "";
//                    String third_class = "";
//                    String fourth_class = "";
//                    List<String> rowValueList = null;
//                    boolean is2018AndBefore = false;
//                    for(int i=0;i<tableRows.size();i++){
//                        Elements tds = tableRows.get(i).select("td");
//                        boolean hasrowspan = false;
//                        boolean hascolspan = false;
//                        rowValueList = new ArrayList<>();
//                        for(int j=0;j<tds.size();j++){
//                            String rowspan = tds.get(j).attr("rowspan");
//                            String colspan = tds.get(j).attr("colspan");
//                            text = tds.get(j).text();
//                            if("1".equals(appParameters.getIsHighPrecision())){
//                                try {
//                                    text = String.valueOf(new BigDecimal(tds.get(j).attr("x:num")).setScale(4, BigDecimal.ROUND_HALF_UP));
//                                } catch (Exception e){
//                                    logger.error("cuowu",e);
//                                    text = tds.get(j).text();
//                                }
//                            }
//                            if(text.contains("月")&&(text.contains("至"))){
//                                is2018AndBefore = true;
//                            }
//                            if (StringUtils.hasLength(rowspan)) {
//                                hasrowspan = true;
//                                continue;
//                            }
//                            if (StringUtils.hasLength(colspan)) {
//                                hascolspan = true;
//                                continue;
//                            }
//
//                            if(j==0){
////                                third_class = text.replace("　", "");
//                                fourth_class = text.replace("　", "");
//                                if (text.startsWith(DataTableConstant.FOURTH_CLASS_FLAG)) {
//                                } else if (text.startsWith(DataTableConstant.THIRD_CLASS_FLAG)) {
//                                    third_class = text.replace("　", "");
//                                } else if (text.startsWith(DataTableConstant.SECOND_CLASS_FLAG)) {
//                                    second_class = text.replace("　", "");
//                                } else {
//                                    third_class = text.replace("　", "");
//                                    second_class = text.replace("　", "");
//                                    first_class = text.replace("　", "");
//                                }
//                            } else {
//                                text = text.replace("　","").replace(",","");
//                                if ("--".equalsIgnoreCase(text)||".".equalsIgnoreCase(text)||".　".equalsIgnoreCase(text)||"-".equalsIgnoreCase(text)) {
//                                    text = "";
//                                }
//                                if(text.contains("人民币")){
//                                    hasrowspan = true;
//                                    break;
//                                }
//                            }
//                            rowValueList.add(j,text);
//                        }
//                        if(hasrowspan){
//                            continue;
//                        }
//                        if(hascolspan){
//                            continue;
//                        }
//                        try{
//
//                            GoodsCategory object = null;
//                            object = new GoodsCategory();
//                            object.setGuid(UUID.randomUUID().toString());
//                            object.setSysDate(new Date());
//                            object.setProvinceName(proviceName);
//                            object.setYearMonthValue(yearMonthValue);
//                            object.setIeType(ieType);
//                            object.setFirstClass(first_class);
//                            object.setSecondClass(second_class);
//                            object.setThirdClass(third_class);
//                            object.setFourthClass(fourth_class);
//                            if (StringUtils.hasLength(rowValueList.get(1))) {
//                                object.setUnitName(rowValueList.get(1));
//                            }
//                            if (is2018AndBefore){
//                                if (StringUtils.hasLength(rowValueList.get(6))) {
//                                    object.setQty(new BigDecimal(rowValueList.get(6)));
//                                }
//                                if (StringUtils.hasLength(rowValueList.get(7))) {
//                                    object.setQtyPercent(new BigDecimal(rowValueList.get(7)));
//                                }
//                                if (StringUtils.hasLength(rowValueList.get(8))) {
//                                    object.setTotalValue(new BigDecimal(rowValueList.get(8)));
//                                }
//                                if (StringUtils.hasLength(rowValueList.get(9))) {
//                                    object.setTotalValuePercent(new BigDecimal(rowValueList.get(9)));
//                                }
//                            } else {
//                                if (StringUtils.hasLength(rowValueList.get(2))) {
//                                    object.setQty(new BigDecimal(rowValueList.get(2)));
//                                }
//                                if (StringUtils.hasLength(rowValueList.get(3))) {
//                                    object.setQtyPercent(new BigDecimal(rowValueList.get(3)));
//                                }
//                                if (StringUtils.hasLength(rowValueList.get(4))) {
//                                    object.setTotalValue(new BigDecimal(rowValueList.get(4)));
//                                }
//                                if (StringUtils.hasLength(rowValueList.get(5))) {
//                                    object.setTotalValuePercent(new BigDecimal(rowValueList.get(5)));
//                                }
//                            }
//                            LambdaQueryWrapper<GoodsCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//                            lambdaQueryWrapper.eq(GoodsCategory::getYearMonthValue,object.getYearMonthValue());
//                            lambdaQueryWrapper.eq(GoodsCategory::getProvinceName,object.getProvinceName());
//                            lambdaQueryWrapper.eq(GoodsCategory::getFirstClass,object.getFirstClass());
//                            lambdaQueryWrapper.eq(GoodsCategory::getSecondClass,object.getSecondClass());
//                            lambdaQueryWrapper.eq(GoodsCategory::getThirdClass,object.getThirdClass());
//                            lambdaQueryWrapper.eq(GoodsCategory::getFourthClass,object.getFourthClass());
//                            lambdaQueryWrapper.eq(GoodsCategory::getIeType,object.getIeType());
//
//                            GoodsCategory dbObject = goodsCategoryMapper.selectOne(lambdaQueryWrapper);
//                            if(null != dbObject){
//                                goodsCategoryMapper.delete(lambdaQueryWrapper);
//                            }
//                            goodsCategoryMapper.insert(object);
//                        } catch (Exception e){
//                            e.printStackTrace();
//                            logger.error(String.format("处理错误，文件：[%s],行：[%s],值：[%s]",title,i,rowValueList),e);
//                            continue;
//                        }
//                    }
//                } else {
//                    String text = "";
//                    String first_class = "";
//                    String second_class = "";
//                    List<String> rowValueList = null;
//                    List<Integer> dataColumnIndexsI = new ArrayList<>();
//                    List<Integer> dataColumnIndexsE = new ArrayList<>();
//                    int skipRowNum = 0;
//                    for(int i=0;i<tableRows.size();i++) {
//                        Elements tds = tableRows.get(i).select("td");
//                        boolean hasrowspan = false;
//                        boolean hascolspan = false;
//                        rowValueList = new ArrayList<>();
//                        int beginIndex = 0;
//                        for (int j = 0; j < tds.size(); j++) {
//                            String rowspan = tds.get(j).attr("rowspan");
//                            String colspan = tds.get(j).attr("colspan");
//                            text = tds.get(j).text();
//                            if("1".equals(appParameters.getIsHighPrecision())){
//                                try {
//                                    text = String.valueOf(new BigDecimal(tds.get(j).attr("x:num")).setScale(4, BigDecimal.ROUND_HALF_UP));
//                                } catch (Exception e){
////                                System.out.println(text);
//                                    text = tds.get(j).text();
//                                }
//                            }
//                            if(i == 0){
//                                if(text.contains("进出口")) {
//                                } else if(text.contains("出口")){
//                                    dataColumnIndexsE.add(beginIndex);
//                                    dataColumnIndexsE.add(beginIndex+1);
//                                } else if(text.contains("进口")){
//                                    dataColumnIndexsI.add(beginIndex);
//                                    dataColumnIndexsI.add(beginIndex+1);
//                                }else {
//                                    beginIndex +=1;
//                                }
//                                if(StringUtils.hasLength(colspan)){
//                                    beginIndex += Integer.valueOf(colspan);
//                                }
//                                if (j == 0 && StringUtils.hasLength(rowspan)) {
//                                    skipRowNum = Integer.valueOf(rowspan);
//                                }
//                            } else {
//                                if (StringUtils.hasLength(rowspan)) {
//                                    hasrowspan = true;
//                                    i += Integer.valueOf(rowspan) - 1;
//                                    break;
//                                }
//                                if (StringUtils.hasLength(colspan)) {
//                                    hascolspan = true;
//                                    continue;
//                                }
//                                if (j == 0) {
//                                    second_class = text.replace("　", "");
//                                    if (text.startsWith(DataTableConstant.FOURTH_CLASS_FLAG)) {
//                                    } else if (text.startsWith(DataTableConstant.THIRD_CLASS_FLAG)) {
//                                    } else if (text.startsWith(DataTableConstant.SECOND_CLASS_FLAG)) {
//                                    } else if (text.startsWith(DataTableConstant.FIRST_CLASS_FLAG)) {
//                                    } else {
//                                        first_class = text.replace("　", "");
//                                    }
//                                } else {
//                                    text = text.replace("　", "").replace(",", "");
//                                    if ("--".equalsIgnoreCase(text)||".".equalsIgnoreCase(text)||".　".equalsIgnoreCase(text)||"-".equalsIgnoreCase(text)) {
//                                        text = "";
//                                    }
//                                    if(text.contains("人民币")){
//                                        hasrowspan = true;
//                                        break;
//                                    }
//                                }
//                                rowValueList.add(j, text);
//                            }
//                        }
//                        if(i<skipRowNum){
//                            continue;
//                        }
//                        if (hasrowspan) {
//                            continue;
//                        }
//                        if (hascolspan) {
//                            continue;
//                        }
//                        try {
//                            if(dataType.equals(DataTableConstant.COMPANY_NATURE)) {
//                                CompanyNature object = null;
//                                object = new CompanyNature();
//                                object.setProvinceName(proviceName);
//                                object.setYearMonthValue(yearMonthValue);
//                                object.setFirstClass(first_class);
//                                object.setSecondClass(second_class);
//
//                                CompanyNature objectI = new CompanyNature();
//                                CompanyNature objectE = new CompanyNature();
//                                BeanUtils.copyProperties(object,objectI);
//                                BeanUtils.copyProperties(object,objectE);
//
//                                objectI.setGuid(UUID.randomUUID().toString());
//                                objectE.setGuid(UUID.randomUUID().toString());
//                                objectI.setSysDate(new Date());
//                                objectE.setSysDate(new Date());
//                                objectI.setIeType("I");
//                                objectE.setIeType("E");
//
//                                if(rowValueList.isEmpty()){
//                                    continue;
//                                }
//                                if(StringUtils.hasLength(rowValueList.get(dataColumnIndexsE.get(0)))){
//                                    objectE.setTotalValue(new BigDecimal(rowValueList.get(dataColumnIndexsE.get(0))));
//                                }
//                                if(StringUtils.hasLength(rowValueList.get(dataColumnIndexsE.get(1)))){
//                                    objectE.setTotalValuePercent(new BigDecimal(rowValueList.get(dataColumnIndexsE.get(1))));
//                                }
//                                if(StringUtils.hasLength(rowValueList.get(dataColumnIndexsI.get(0)))){
//                                    objectI.setTotalValue(new BigDecimal(rowValueList.get(dataColumnIndexsI.get(0))));
//                                }
//                                if(StringUtils.hasLength(rowValueList.get(dataColumnIndexsI.get(1)))){
//                                    objectI.setTotalValuePercent(new BigDecimal(rowValueList.get(dataColumnIndexsI.get(1))));
//                                }
//                                LambdaQueryWrapper<CompanyNature> lambdaQueryWrapperI = new LambdaQueryWrapper<>();
//                                lambdaQueryWrapperI.eq(CompanyNature::getYearMonthValue,objectI.getYearMonthValue());
//                                lambdaQueryWrapperI.eq(CompanyNature::getProvinceName,objectI.getProvinceName());
//                                lambdaQueryWrapperI.eq(CompanyNature::getFirstClass,objectI.getFirstClass());
//                                lambdaQueryWrapperI.eq(CompanyNature::getSecondClass,objectI.getSecondClass());
//                                lambdaQueryWrapperI.eq(CompanyNature::getIeType,objectI.getIeType());
//
//                                CompanyNature dbObjectI = companyNatureMapper.selectOne(lambdaQueryWrapperI);
//                                if(null != dbObjectI){
//                                    companyNatureMapper.delete(lambdaQueryWrapperI);
//                                }
//                                companyNatureMapper.insert(objectI);
//
//                                LambdaQueryWrapper<CompanyNature> lambdaQueryWrapperE = new LambdaQueryWrapper<>();
//                                lambdaQueryWrapperE.eq(CompanyNature::getYearMonthValue,objectE.getYearMonthValue());
//                                lambdaQueryWrapperE.eq(CompanyNature::getProvinceName,objectE.getProvinceName());
//                                lambdaQueryWrapperE.eq(CompanyNature::getFirstClass,objectE.getFirstClass());
//                                lambdaQueryWrapperE.eq(CompanyNature::getSecondClass,objectE.getSecondClass());
//                                lambdaQueryWrapperE.eq(CompanyNature::getIeType,objectE.getIeType());
//
//                                CompanyNature dbObjectE = companyNatureMapper.selectOne(lambdaQueryWrapperE);
//                                if(null != dbObjectE){
//                                    companyNatureMapper.delete(lambdaQueryWrapperE);
//                                }
//                                companyNatureMapper.insert(objectE);
//                            } else if(dataType.equals(DataTableConstant.COUNTRY_AREA)) {
//                                CountryArea object = null;
//                                object = new CountryArea();
//                                object.setProvinceName(proviceName);
//                                object.setYearMonthValue(yearMonthValue);
//                                object.setFirstClass(first_class);
//                                object.setSecondClass(second_class);
//
//                                CountryArea objectI = new CountryArea();
//                                CountryArea objectE = new CountryArea();
//                                BeanUtils.copyProperties(object,objectI);
//                                BeanUtils.copyProperties(object,objectE);
//
//                                objectI.setGuid(UUID.randomUUID().toString());
//                                objectE.setGuid(UUID.randomUUID().toString());
//                                objectI.setSysDate(new Date());
//                                objectE.setSysDate(new Date());
//                                objectI.setIeType("I");
//                                objectE.setIeType("E");
//
//                                if(rowValueList.isEmpty()){
//                                    continue;
//                                }
//                                if(StringUtils.hasLength(rowValueList.get(dataColumnIndexsE.get(0)))){
//                                    objectE.setTotalValue(new BigDecimal(rowValueList.get(dataColumnIndexsE.get(0))));
//                                }
//                                if(StringUtils.hasLength(rowValueList.get(dataColumnIndexsE.get(1)))){
//                                    objectE.setTotalValuePercent(new BigDecimal(rowValueList.get(dataColumnIndexsE.get(1))));
//                                }
//                                if(StringUtils.hasLength(rowValueList.get(dataColumnIndexsI.get(0)))){
//                                    objectI.setTotalValue(new BigDecimal(rowValueList.get(dataColumnIndexsI.get(0))));
//                                }
//                                if(StringUtils.hasLength(rowValueList.get(dataColumnIndexsI.get(1)))){
//                                    objectI.setTotalValuePercent(new BigDecimal(rowValueList.get(dataColumnIndexsI.get(1))));
//                                }
//
//                                LambdaQueryWrapper<CountryArea> lambdaQueryWrapperI = new LambdaQueryWrapper<>();
//                                lambdaQueryWrapperI.eq(CountryArea::getYearMonthValue,objectI.getYearMonthValue());
//                                lambdaQueryWrapperI.eq(CountryArea::getProvinceName,objectI.getProvinceName());
//                                lambdaQueryWrapperI.eq(CountryArea::getFirstClass,objectI.getFirstClass());
//                                lambdaQueryWrapperI.eq(CountryArea::getSecondClass,objectI.getSecondClass());
//                                lambdaQueryWrapperI.eq(CountryArea::getIeType,objectI.getIeType());
//
//                                CountryArea dbObjectI = countryAreaMapper.selectOne(lambdaQueryWrapperI);
//                                if(null != dbObjectI){
//                                    countryAreaMapper.delete(lambdaQueryWrapperI);
//                                }
//                                countryAreaMapper.insert(objectI);
//
//                                LambdaQueryWrapper<CountryArea> lambdaQueryWrapperE = new LambdaQueryWrapper<>();
//                                lambdaQueryWrapperE.eq(CountryArea::getYearMonthValue,objectE.getYearMonthValue());
//                                lambdaQueryWrapperE.eq(CountryArea::getProvinceName,objectE.getProvinceName());
//                                lambdaQueryWrapperE.eq(CountryArea::getFirstClass,objectE.getFirstClass());
//                                lambdaQueryWrapperE.eq(CountryArea::getSecondClass,objectE.getSecondClass());
//                                lambdaQueryWrapperE.eq(CountryArea::getIeType,objectE.getIeType());
//
//                                CountryArea dbObjectE = countryAreaMapper.selectOne(lambdaQueryWrapperE);
//                                if(null != dbObjectE){
//                                    countryAreaMapper.delete(lambdaQueryWrapperE);
//                                }
//                                countryAreaMapper.insert(objectE);
//                            } else if(dataType.equals(DataTableConstant.TRADE_MODE)) {
//                                TradeMode object = null;
//                                object = new TradeMode();
//                                object.setProvinceName(proviceName);
//                                object.setYearMonthValue(yearMonthValue);
//                                object.setFirstClass(first_class);
//                                object.setSecondClass(second_class);
//
//                                TradeMode objectI = new TradeMode();
//                                TradeMode objectE = new TradeMode();
//                                BeanUtils.copyProperties(object,objectI);
//                                BeanUtils.copyProperties(object,objectE);
//
//                                objectI.setGuid(UUID.randomUUID().toString());
//                                objectE.setGuid(UUID.randomUUID().toString());
//                                objectI.setSysDate(new Date());
//                                objectE.setSysDate(new Date());
//                                objectI.setIeType("I");
//                                objectE.setIeType("E");
//
//                                if(rowValueList.isEmpty()){
//                                    continue;
//                                }
//                                if(StringUtils.hasLength(rowValueList.get(dataColumnIndexsE.get(0)))){
//                                    objectE.setTotalValue(new BigDecimal(rowValueList.get(dataColumnIndexsE.get(0))));
//                                }
//                                if(StringUtils.hasLength(rowValueList.get(dataColumnIndexsE.get(1)))){
//                                    objectE.setTotalValuePercent(new BigDecimal(rowValueList.get(dataColumnIndexsE.get(1))));
//                                }
//                                if(StringUtils.hasLength(rowValueList.get(dataColumnIndexsI.get(0)))){
//                                    objectI.setTotalValue(new BigDecimal(rowValueList.get(dataColumnIndexsI.get(0))));
//                                }
//                                if(StringUtils.hasLength(rowValueList.get(dataColumnIndexsI.get(1)))){
//                                    objectI.setTotalValuePercent(new BigDecimal(rowValueList.get(dataColumnIndexsI.get(1))));
//                                }
//
//                                LambdaQueryWrapper<TradeMode> lambdaQueryWrapperI = new LambdaQueryWrapper<>();
//                                lambdaQueryWrapperI.eq(TradeMode::getYearMonthValue,objectI.getYearMonthValue());
//                                lambdaQueryWrapperI.eq(TradeMode::getProvinceName,objectI.getProvinceName());
//                                lambdaQueryWrapperI.eq(TradeMode::getFirstClass,objectI.getFirstClass());
//                                lambdaQueryWrapperI.eq(TradeMode::getSecondClass,objectI.getSecondClass());
//                                lambdaQueryWrapperI.eq(TradeMode::getIeType,objectI.getIeType());
//
//                                TradeMode dbObjectI = tradeModeMapper.selectOne(lambdaQueryWrapperI);
//                                if(null != dbObjectI){
//                                    tradeModeMapper.delete(lambdaQueryWrapperI);
//                                }
//                                tradeModeMapper.insert(objectI);
//
//                                LambdaQueryWrapper<TradeMode> lambdaQueryWrapperE = new LambdaQueryWrapper<>();
//                                lambdaQueryWrapperE.eq(TradeMode::getYearMonthValue,objectE.getYearMonthValue());
//                                lambdaQueryWrapperE.eq(TradeMode::getProvinceName,objectE.getProvinceName());
//                                lambdaQueryWrapperE.eq(TradeMode::getFirstClass,objectE.getFirstClass());
//                                lambdaQueryWrapperE.eq(TradeMode::getSecondClass,objectE.getSecondClass());
//                                lambdaQueryWrapperE.eq(TradeMode::getIeType,objectE.getIeType());
//
//                                TradeMode dbObjectE = tradeModeMapper.selectOne(lambdaQueryWrapperE);
//                                if(null != dbObjectE){
//                                    tradeModeMapper.delete(lambdaQueryWrapperE);
//                                }
//                                tradeModeMapper.insert(objectE);
//                            }
//                        } catch (Exception e){
//                            e.printStackTrace();
//                            logger.error(String.format("处理错误，文件：[%s],行：[%s],值：[%s]",title,i,rowValueList));
//                            continue;
//                        }
//                    }
//                }
//            }
            return true;
        } catch (Exception e){
            logger.error(e.getMessage(),e);
            return false;
        }
    }


    public boolean handle_zz(String payload){
        return false;
    }
    public boolean handle_zs(String payload) {
        try {
//            String str = FileUtils.readFileToString(new File(dir + File.separator + src), "UTF-8");
            Document document = Jsoup.parse(payload);
            // 获取标题
            Elements body = document.getElementsByTag("body");
            String bodyHtml = body.get(0).html();
            String title = bodyHtml.substring(0, bodyHtml.indexOf("<br>")).trim();
            logger.info(String.format("开始处理,标题为[%s]",title));
//            System.out.println(String.format("开始处理,标题为[%s]",title));
            /**
             * 1 获取省份 有zz 无 zs
             * 2 年月
             * 3 判断数据类型
             * 4 处理
             *
             */
            String proviceName = "河南省";
            String dataSrc = DataTableConstant.DATA_SRC_ZZ;
            if (!title.contains(proviceName)) {
                proviceName = null;
                dataSrc= DataTableConstant.DATA_SRC_ZS;
            }
            logger.info(String.format("分析标题,省份为[%s]",proviceName));
            String yearMonthValue = getYearMonthFromTitle(title);
            logger.info(String.format("分析标题,年月为[%s]",yearMonthValue));
            String ieType = "";
            if (!title.contains("进出口")) {
                if (title.contains("进口")) {
                    ieType = "I";
                } else if (title.contains("出口")) {
                    ieType = "E";
                }
            }
//            logger.info(String.format("分析标题,进出口类型为[%s]",ieType));
            int skipRowNum = 0;
            String dataType = DataTableConstant.OTHER_TYPE;
            if(StringUtils.hasLength(proviceName)){
                // 处理郑州海关数据
                skipRowNum = 0;
                if (title.contains(DataTableConstant.GOODS_CATEGORY)) {
                    dataType = DataTableConstant.DATA_TYPE_ZZ_GOODS_CATEGORY;
                } else if (title.contains(DataTableConstant.COMPANY_NATURE)) {
                    dataType = DataTableConstant.DATA_TYPE_ZZ_COMPANY_NATURE;
                } else if (title.contains(DataTableConstant.COUNTRY_AREA)) {
                    dataType = DataTableConstant.DATA_TYPE_ZZ_COUNTRY_AREA;
                } else if (title.contains(DataTableConstant.TRADE_MODE)) {
                    dataType = DataTableConstant.DATA_TYPE_ZZ_TRADE_MODE;
                } else if (DataTableConstant.OTHER_TYPE.equals(dataType)) {
                }
            } else {
                // 处理海关总署数据
                dataType = DataTableConstant.DATA_TYPE_ZS_IN_DIST_SRC_ADDRESS;
                skipRowNum = 1;
                if(title.contains(DataTableConstant.ZS_NIANDU)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_NIANDU;
                } else if(title.contains(DataTableConstant.ZS_YUEDU)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_YUEDU;
                } else if(title.contains(DataTableConstant.ZS_COUNTRY)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_COUNTRY;
                } else if(title.contains(DataTableConstant.ZS_GOODS_CONSTITUTE)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_GOODS_CONSTITUTE;
                } else if(title.contains(DataTableConstant.ZS_TRADEMODE_COMNATURE)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_TRADEMODE_COMNATURE;
                } else if(title.contains(DataTableConstant.ZS_PRIMARY_TRADEMODE)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_SERIVAL_PRIMARY_TRADEMODE;
                } else if(title.contains(DataTableConstant.ZS_TRADE_MODE)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_TRADE_MODE;
                } else if(title.contains(DataTableConstant.ZS_COMPANY_ADDRESS)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_COMPANY_ADDRESS;
                } else if(title.contains(DataTableConstant.ZS_IN_DIST_SRC_ADDRESS)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_IN_DIST_SRC_ADDRESS;
                } else if(title.contains(DataTableConstant.ZS_GOODS_CUSTOMS)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_GOODS_CUSTOMS;
                } else if(title.contains(DataTableConstant.ZS_SPECTIAL_ADDRESS)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_SPECTIAL_ADDRESS;
                } else if(title.contains(DataTableConstant.ZS_FOREIGN_INVESTMENT_COMPANY)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_FOREIGN_INVESTMENT_COMPANY;
                } else if(title.contains(DataTableConstant.ZS_GOODS_PRIMARY)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_GOODS_PRIMARY;
                } else if(title.contains(DataTableConstant.ZS_SERIVAL_COUNTRY)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_SERIVAL_COUNTRY;
                } else if(title.contains(DataTableConstant.ZS_GOODS_CLASS)){
                    dataType = DataTableConstant.DATA_TYPE_ZS_GOODS_CLASS;
                } else {
                    dataType = DataTableConstant.OTHER_TYPE;
                }
            }
            System.out.println(String.format("开始处理类型[%s]，原文件名：[%s]", dataType, title));
            // 获取表格
            Elements tableRows = document.getElementsByTag("table").get(0).select("tr");
            if (tableRows.size() <= 1) {
//                logger.error(String.format("没有找到有效的表格数据"));
            } else {
                CommonDomain commonDomain = new CommonDomain();
                String text = "";
                String first_class = ""; //第一列内容
                String second_class = ""; // 行标题
                String third_class = "";  // 单位
                String fourth_class = "";  // 四级
                String value_1 = "";    //金额
                String value_2 = "";    //金额占比
                String value_3 = "";    //数量
                String value_4 = "";    //数量占比
                String note = "";
                String note_1 = "";
                String note_2 = title;
                String unit = "";
                String nature = "";
                int maxSizePerRow = 0; //每行最大列数
                int curSizePerRow = 0;

                Map<Integer, Map<Integer, String>> table_map = new HashMap<>();
                Map<Integer, String> col_map = new HashMap<>();

                Map<Integer, Map<Integer, String>> skipcell_map = new HashMap<>();
                Map<Integer, String> skipcol_map = new HashMap<>();
                Map<Integer, String> last_col_map = new HashMap<>();

//                for(int i=0;i<rowSize;i++){
                int swithNumIndex = 0;
//                取出前4行
                int maxTestRowSize = 4;

                for (int i = 0; i < maxTestRowSize; i++) {
                    col_map = table_map.get(i);
                    if (Objects.isNull(col_map)) {
                        col_map = new HashMap<>();
                        table_map.put(i, col_map);
                    }
                    Elements tds = tableRows.get(i).select("td");
                    curSizePerRow = tds.size();
                    maxSizePerRow = Math.max(curSizePerRow, maxSizePerRow);
                    swithNumIndex = 0;
                    int correctionalIndex = 0;
                    last_col_map = new HashMap<>();
                    for (int j = 0; j < curSizePerRow; j++) {
                        Element td = tds.get(j);
                        String rowspan = td.attr("rowspan");
                        String colspan = td.attr("colspan");
                        text = td.attr("x:num");
                        if (StringUtils.hasLength(text)) {
                            text = String.valueOf(new BigDecimal(text).setScale(4, BigDecimal.ROUND_HALF_UP));
                        } else {
                            text = td.text();
                        }
                        text = text.replace("　", "").trim();
                        if ("-".equals(text) || "—".equals(text)) {
                            text = "";
                        }
                        correctionalIndex = swithNumIndex + j;
                        maxSizePerRow = Math.max((correctionalIndex + 1), maxSizePerRow);

                        col_map.put(correctionalIndex, text);
                        if (StringUtils.hasLength(rowspan)) {
                            Map<Integer, String> temp;
                            for (int m = 1; m < Integer.parseInt(rowspan); m++) {
                                temp = table_map.get(i + m);
                                if (Objects.isNull(temp)) {
                                    temp = new HashMap<>();
                                    table_map.put(i + m, temp);
                                }
                                // 标记合并后修复的单元格坐标
                                skipcol_map = skipcell_map.get(i + m);
                                if (Objects.isNull(skipcol_map)) {
                                    skipcol_map = new HashMap<>();
                                    skipcell_map.put(i + m, skipcol_map);
                                }
                                skipcol_map.put(correctionalIndex, "");
                            }
                        }
                        if (StringUtils.hasLength(colspan)) {
                            for (int m = 1; m < Integer.parseInt(colspan); m++) {
//                                col_map.put(correctionalIndex+m,"");
                                skipcol_map = skipcell_map.get(i);
                                if (Objects.isNull(skipcol_map)) {
                                    skipcol_map = new HashMap<>();
                                    skipcell_map.put(i, skipcol_map);
                                }
//                                skipcol_map.put(correctionalIndex + m, ""); //标记合并单元格为空
                                col_map.put(correctionalIndex+m,text);  //填充合并单元格的其他格值
                            }
                            swithNumIndex += Integer.parseInt(colspan) - 1;
                        }
                    }
                }
//                debug 时用
//                System.out.println("-------------------");
//                for (Integer key : skipcell_map.keySet()) {
//                    for (Integer key2 : skipcell_map.get(key).keySet()) {
//                        System.out.print(key + "," + key2 + "," + skipcell_map.get(key).get(key2));
//                        System.out.print("\t");
//                    }
//                    System.out.println("");
//                }
//                System.out.println("-------------------");
//                for (Integer key : table_map.keySet()) {
//                    for (Integer key2 : table_map.get(key).keySet()) {
//                        System.out.print(key + "," + key2 + "," + table_map.get(key).get(key2));
//                        System.out.print("\t");
//                    }
//                    System.out.println("");
//                }
//                System.out.println("====================");

                List<String[]> colNameLists = new ArrayList<>();
                String[] colNames = new String[maxSizePerRow];
                String titleName;
                Integer tempIndex = 0;
//                修正前4行数据
                Map<Integer, String> temp_col_map = new HashMap<>();
                for (int i = 0; i < maxTestRowSize; i++) {
                    String[] tempcolNames = new String[maxSizePerRow];;
                    skipcol_map = skipcell_map.get(i);
                    col_map = table_map.get(i);
                    if(null==skipcol_map||null==col_map){
                        continue;
                    }
                    if (col_map.size() + skipcol_map.size() == maxSizePerRow) {
                        temp_col_map = new HashMap<>();
                        tempIndex = 0;
                        for (int j = 0; j < maxSizePerRow; j++) {

                            if (skipcol_map.containsKey(j)) {
                                titleName = skipcol_map.get(j);
                                tempIndex++;
                                if(j-tempIndex>0&&j>0&&!skipcol_map.containsKey(j+1)){
                                    tempIndex--;
                                }
                            } else {
                                titleName = col_map.get(j - tempIndex);
                                if (null == titleName) {
                                    titleName = col_map.get(j);
//                                    tempIndex--;
                                }
                            }
                            if(i>skipRowNum&&table_map.containsKey(i-1)){
                                last_col_map = table_map.get(i-1);
                                if(!Objects.isNull(last_col_map)){
                                    if(last_col_map.containsKey(j)){
                                        titleName = last_col_map.get(j)+";" + titleName;
                                    }
                                }
                            }
                            if (StringUtils.hasLength(titleName)) {
                                colNames[j] = titleName;
                            }
                            temp_col_map.put(j,titleName);
                        }
                        table_map.put(i,temp_col_map);
                    }
                    System.arraycopy(colNames,0,tempcolNames,0,maxSizePerRow);
                    colNameLists.add(tempcolNames);
                    for (int m = 0; m < maxSizePerRow; m++) {
//                        System.out.print(tempcolNames[m] + "__\t");
                    }
//                    System.out.println();
                }
//                System.out.println(colNameLists);
                Map<Integer,String> wantedColName = new HashMap<>();
                Map<Integer,String> wantedColValue = new HashMap<>();
                Map<Integer,String> wantedColName_I = new HashMap<>();
                Map<Integer,String> wantedColValue_I = new HashMap<>();
                Map<Integer,String> wantedColName_E = new HashMap<>();
                Map<Integer,String> wantedColValue_E = new HashMap<>();
                int nameRowNum=1;
                int valueRowNum=2;
                int dataStartRowNum = 0;

                String tempLastColName = null;

                // 设定标题和值行
                if(dataType.equals(DataTableConstant.DATA_TYPE_ZZ_GOODS_CATEGORY)){
                    nameRowNum=0;
                    valueRowNum=1;
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZZ_COMPANY_NATURE)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZZ_COUNTRY_AREA)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZZ_TRADE_MODE)){

                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_NIANDU)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_YUEDU)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_COUNTRY)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_GOODS_CONSTITUTE)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_GOODS_CLASS)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_TRADE_MODE)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_TRADEMODE_COMNATURE)){
                    nameRowNum=2;
                    valueRowNum=3;
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_COMPANY_ADDRESS)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_IN_DIST_SRC_ADDRESS)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_GOODS_CUSTOMS)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_SPECTIAL_ADDRESS)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_FOREIGN_INVESTMENT_COMPANY)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_GOODS_PRIMARY)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_SERIVAL_COUNTRY)){
                } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_SERIVAL_PRIMARY_TRADEMODE)){
                } else if(dataType.equals(DataTableConstant.OTHER_TYPE)){
                }

                dataStartRowNum = valueRowNum+1;
                String[] tempColNames = colNameLists.get(nameRowNum);
                String[] tempColValues = colNameLists.get(valueRowNum);
                // 获取数据列名称和坐标
                for(int i=0;i<tempColNames.length;i++){
                    String tempColName = tempColNames[i];
                    String tempColValue = tempColValues[i];
                    if(!StringUtils.hasLength(tempColName)){
                        tempColName=tempLastColName;
                    }
                    tempLastColName = tempColName;
                    if(tempColName.contains("进出口")||tempColName.contains("至")||tempColName.contains("累计")||tempColName.contains("合计")||tempColName.contains("小计")||tempColName.contains("人民币")||tempColName.contains("贸易差额")||(tempColName.contains("月")&&(tempColName.contains("-")||tempColName.contains("至")))){
                        continue;
                    }
                    if(tempColValue.contains("进出口")){
                        continue;
                    }
                    if(StringUtils.hasLength(tempColValue)&&tempColValue.contains("月")&&((tempColValue.contains("至")||tempColValue.contains("-")))){
                        continue;
                    }
                    if(!StringUtils.hasLength(ieType)){
                        if(tempColName.contains("进口")){
                            wantedColValue_I.put(i,tempColValue);
                            wantedColName_I.put(i,tempColName);
                        }else if(tempColName.contains("出口")){
                            wantedColValue_E.put(i,tempColValue);
                            wantedColName_E.put(i,tempColName);
                        } else {
                            if(tempColValue.contains("进口")){
                                wantedColValue_I.put(i,tempColValue);
                                wantedColName_I.put(i,tempColName);
                            }else if(tempColValue.contains("出口")){
                                wantedColValue_E.put(i,tempColValue);
                                wantedColName_E.put(i,tempColName);
                            }else {
                                wantedColValue_I.put(i,tempColValue);
                                wantedColName_I.put(i,tempColName);
                                wantedColValue_E.put(i,tempColValue);
                                wantedColName_E.put(i,tempColName);
                            }
                        }
                    } else {
                        wantedColValue.put(i,tempColValue);
                        wantedColName.put(i,tempColName);
                    }
                }

//                for(Integer key:wantedColName.keySet()){
//                    System.out.println(key+"-"+wantedColName.get(key)+"-"+wantedColValue.get(key));
//                }
//                for(Integer key:wantedColName_I.keySet()){
//                    System.out.println(key+"-"+wantedColName_I.get(key)+"-"+wantedColValue_I.get(key));
//                }
//                for(Integer key:wantedColName_E.keySet()){
//                    System.out.println(key+"-"+wantedColName_E.get(key)+"-"+wantedColValue_E.get(key));
//                }

                List<Map<Integer,String>[]> wantedLists = new ArrayList<>();
                wantedLists.add(new Map[]{wantedColName, wantedColValue});
                wantedLists.add(new Map[]{wantedColName_I, wantedColValue_I});
                wantedLists.add(new Map[]{wantedColName_E, wantedColValue_E});

                for (int i = dataStartRowNum; i < tableRows.size(); i++) {
                    col_map = table_map.get(i);
                    if (Objects.isNull(col_map)) {
                        col_map = new HashMap<>();
                        table_map.put(i, col_map);
                    }

                    Elements tds = tableRows.get(i).select("td");
                    curSizePerRow = tds.size();
                    maxSizePerRow = Math.max(curSizePerRow, maxSizePerRow);
                    swithNumIndex = 0;
                    int correctionalIndex = 0;
                    int map_size = 0;
                    Map<Integer,String> colNameMap = null;
                    Map<Integer,String> colValueMap = null;
                    boolean isHandled = false;
                    boolean isSkipCol_0 = false;
                    for(Map<Integer,String>[] colNameMaps:wantedLists){
                        if(Objects.isNull(colNameMaps)){
                            continue;
                        }
                        colNameMap = colNameMaps[0];
                        colValueMap = colNameMaps[1];
                        correctionalIndex = 0;
                        map_size = colNameMap.size();
                        int specFlag = 0; //作用于同时还有数量和金额
                        for(Integer key:colNameMap.keySet()){
                            map_size --;
                            Element td = null;
                            try{
                                td = tds.get(key);
                            } catch (Exception e){
                                e.printStackTrace();
                                break;
                            }
                            String rowspan = td.attr("rowspan");
                            if (StringUtils.hasLength(rowspan)) {   //跳过合并行的下一行
                                if(0==correctionalIndex&&!isHandled){
                                    i += (Integer.parseInt(rowspan)-1);
                                    correctionalIndex = i;
                                    isHandled = true;
                                }
                            }
                            text = td.attr("x:num");
                            if (StringUtils.hasLength(text)) {
                                text = String.valueOf(new BigDecimal(text).setScale(4, BigDecimal.ROUND_HALF_UP));
                            } else {
                                text = td.text();
                            }
                            if(key==0){
                                if(!isSkipCol_0){
                                    //添加不同类别的处理
                                    if(dataType.equals(DataTableConstant.DATA_TYPE_ZZ_GOODS_CATEGORY)) {
                                        if (text.startsWith(DataTableConstant.FOURTH_CLASS_FLAG)) {
                                            fourth_class = text.replace("　", "");
                                        } else if (text.startsWith(DataTableConstant.THIRD_CLASS_FLAG)) {
                                            fourth_class = null;
                                            third_class = text.replace("　", "");
                                        } else if (text.startsWith(DataTableConstant.SECOND_CLASS_FLAG)) {
                                            fourth_class = null;
                                            third_class = null;
                                            second_class = text.replace("　", "");
                                        } else if (text.startsWith(DataTableConstant.FIRST_CLASS_FLAG)) {
                                            fourth_class = null;
                                            third_class = null;
                                            second_class = null;
                                            first_class = text.replace("　", "");
                                        } else {
                                            fourth_class = null;
                                            third_class = null;
                                            second_class = null;
                                            first_class = text.replace("　", "");
                                        }
                                    } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZZ_COMPANY_NATURE)||dataType.equals(DataTableConstant.DATA_TYPE_ZZ_TRADE_MODE)||dataType.equals(DataTableConstant.DATA_TYPE_ZZ_COUNTRY_AREA)){
                                        if (text.startsWith(DataTableConstant.THIRD_CLASS_FLAG)) {
                                            fourth_class = text.replace("　", "");
                                        } else if (text.startsWith(DataTableConstant.SECOND_CLASS_FLAG)) {
                                            fourth_class = null;
                                            third_class = text.replace("　", "");
                                        } else if (text.startsWith(DataTableConstant.FIRST_CLASS_FLAG)) {
                                            fourth_class = null;
                                            third_class = null;
                                            second_class = text.replace("　", "");
                                        } else {
                                            fourth_class = null;
                                            third_class = null;
                                            second_class = null;
                                            first_class = text.replace("　", "");
                                        }
                                    } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_GOODS_CONSTITUTE)){
                                        if (text.contains("章")) {
                                            third_class = text.replace("　", "");
                                        } else if (text.contains("类")) {
                                            third_class = null;
                                            second_class = text.replace("　", "");
                                        } else {
                                            third_class = null;
                                            second_class = null;
                                            first_class = text.replace("　", "");
                                        }
                                    } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_SPECTIAL_ADDRESS)){
                                        if(StringUtils.hasLength(first_class)&&text.contains(first_class)){
                                            second_class = text.replace("　", "");
                                        } else {
                                            second_class = null;
                                            first_class = text.replace("　", "");
                                        }
                                    } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_GOODS_CLASS)||dataType.equals(DataTableConstant.DATA_TYPE_ZS_SERIVAL_COUNTRY)){
                                        if (text.contains("章")) {
                                            second_class = text.replace("　", "");
                                        } else {
                                            second_class = null;
                                            first_class = text.replace("　", "");
                                        }
                                    } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_FOREIGN_INVESTMENT_COMPANY)){
                                        if (text.contains("企业")) {
                                            second_class = text.replace("　", "");
                                        } else {
                                            second_class = null;
                                            first_class = text.replace("　", "");
                                        }
                                    } else {
                                        if(!StringUtils.hasLength(text)||text.contains("其中")){   // 零列 为空或含有（其中）时，跳过本行
                                            break;
                                        }
                                        if (text.startsWith(DataTableConstant.FOURTH_CLASS_FLAG)) {
                                            fourth_class = text.replace("　", "");
                                        } else if (text.startsWith(DataTableConstant.THIRD_CLASS_FLAG)) {
                                            fourth_class = null;
                                            third_class = text.replace("　", "");
                                        } else if (text.startsWith(DataTableConstant.SECOND_CLASS_FLAG)) {
                                            fourth_class = null;
                                            third_class = null;
                                            second_class = text.replace("　", "");
                                        } else if (text.startsWith(DataTableConstant.FIRST_CLASS_FLAG)) {
                                            fourth_class = null;
                                            third_class = null;
                                            second_class = null;
                                            first_class = text.replace("　", "");
                                        } else {
                                            fourth_class = null;
                                            third_class = null;
                                            second_class = null;
                                            first_class = text.replace("　", "");
                                        }
                                    }
                                }
                                isSkipCol_0 = true;
                                continue;
                            }
                            text = text.replace("　", "").trim().replace(",","");
                            if ("-".equals(text) || "—".equals(text)|| "--".equals(text)||DataTableConstant.BLANK_SPACE_1.equals(text)) {
                                text = "";
                            }
                            String tempColName = colNameMap.get(key);
                            String tempColValue = colValueMap.get(key);
//                                nature = tempColName;
                            if(key==1&&tempColName.contains("计量单位")){    //  第二列有计量单位时为特殊表，
                                unit = text;
                                continue;
                            }
                            if(tempColValue.contains("同比")||tempColValue.contains("同期")){
                                if(tempColValue.contains("数量")){
                                    value_4 = text;
                                } else {
                                    value_2 = text;
                                }
                            } else {
                                if(tempColValue.contains("数量")){
                                    value_3 = text;
                                } else {
                                    value_1 = text;
                                }
                                specFlag ++;
                            }
                            if(tempColName.contains("进口")){
                                ieType = "I";
                            } else if(tempColName.contains("出口")){
                                ieType = "E";
                            }
                            if(map_size>0){
                                if(!StringUtils.hasLength(proviceName)&&(dataType.equals(DataTableConstant.DATA_TYPE_ZS_TRADEMODE_COMNATURE)||dataType.equals(DataTableConstant.DATA_TYPE_ZS_SERIVAL_PRIMARY_TRADEMODE)||dataType.equals(DataTableConstant.DATA_TYPE_ZS_SERIVAL_COUNTRY))){
                                    if(tempColName.contains(";")){
                                        String [] tempColNameArray = tempColName.split(";");
                                        if(StringUtils.hasLength(tempColNameArray[tempColNameArray.length-1])){
                                            nature = tempColNameArray[tempColNameArray.length-1];
                                        } else {
                                            nature = tempColNameArray[tempColNameArray.length-2];
                                        }
                                    } else {
                                        nature = tempColName;
                                    }
                                    if(dataType.equals(DataTableConstant.DATA_TYPE_ZS_SERIVAL_PRIMARY_TRADEMODE)&&specFlag<2){// 同时还有数量和金额的再次添加
                                        continue;
                                    }
                                    specFlag = 0;
                                } else {
                                    continue;
                                }
                            }

                            //添加数据最终处理过程
                            if(dataType.equals(DataTableConstant.DATA_TYPE_ZZ_GOODS_CATEGORY)){
                                GoodsCategory object = null;
                                object = new GoodsCategory();
                                object.setGuid(UUID.randomUUID().toString());
                                object.setSysDate(new Date());
                                object.setProvinceName(proviceName);
                                object.setYearMonthValue(yearMonthValue);
                                object.setIeType(ieType);
                                object.setFirstClass(first_class);
                                object.setSecondClass(second_class);
                                object.setThirdClass(third_class);
                                object.setFourthClass(fourth_class);

                                LambdaQueryWrapper<GoodsCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                                lambdaQueryWrapper.eq(GoodsCategory::getYearMonthValue,object.getYearMonthValue());
                                lambdaQueryWrapper.eq(GoodsCategory::getProvinceName,object.getProvinceName());
                                lambdaQueryWrapper.eq(GoodsCategory::getFirstClass,object.getFirstClass());
                                lambdaQueryWrapper.eq(GoodsCategory::getSecondClass,object.getSecondClass());
                                lambdaQueryWrapper.eq(GoodsCategory::getThirdClass,object.getThirdClass());
                                lambdaQueryWrapper.eq(GoodsCategory::getFourthClass,object.getFourthClass());
                                lambdaQueryWrapper.eq(GoodsCategory::getIeType,object.getIeType());

                                object.setUnitName(unit);
                                if(StringUtils.hasLength(value_3)){
                                    try{
                                        object.setQty(new BigDecimal(value_3));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(StringUtils.hasLength(value_4)){
                                    try{
                                        object.setQtyPercent(new BigDecimal(value_4));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(StringUtils.hasLength(value_1)){
                                    try{
                                        object.setTotalValue(new BigDecimal(value_1));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(StringUtils.hasLength(value_2)){
                                    try{
                                        object.setTotalValuePercent(new BigDecimal(value_2));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(!StringUtils.hasLength(value_1)&&!StringUtils.hasLength(value_2)&&!StringUtils.hasLength(value_3)&&!StringUtils.hasLength(value_3)){
                                    continue;
                                }

                                GoodsCategory dbObject = goodsCategoryMapper.selectOne(lambdaQueryWrapper);
                                if(null != dbObject){
                                    goodsCategoryMapper.delete(lambdaQueryWrapper);
                                }
                                goodsCategoryMapper.insert(object);

                                logger.debug(object.toString());
                            } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZZ_COMPANY_NATURE)){
                                CompanyNature object = null;
                                object = new CompanyNature();
                                object.setProvinceName(proviceName);
                                object.setYearMonthValue(yearMonthValue);
                                object.setFirstClass(first_class);
                                object.setSecondClass(second_class);

                                object.setGuid(UUID.randomUUID().toString());
                                object.setSysDate(new Date());
                                object.setIeType(ieType);

                                if(StringUtils.hasLength(value_1)){
                                    try{
                                        object.setTotalValue(new BigDecimal(value_1));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(StringUtils.hasLength(value_2)){
                                    try{
                                        object.setTotalValuePercent(new BigDecimal(value_2));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(!StringUtils.hasLength(value_1)&&!StringUtils.hasLength(value_2)&&!StringUtils.hasLength(value_3)&&!StringUtils.hasLength(value_3)){
                                    continue;
                                }

                                LambdaQueryWrapper<CompanyNature> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                                lambdaQueryWrapper.eq(CompanyNature::getYearMonthValue,object.getYearMonthValue());
                                lambdaQueryWrapper.eq(CompanyNature::getProvinceName,object.getProvinceName());
                                lambdaQueryWrapper.eq(CompanyNature::getFirstClass,object.getFirstClass());
                                lambdaQueryWrapper.eq(CompanyNature::getSecondClass,object.getSecondClass());
                                lambdaQueryWrapper.eq(CompanyNature::getIeType,object.getIeType());

                                CompanyNature dbObject = companyNatureMapper.selectOne(lambdaQueryWrapper);
                                if(null != dbObject){
                                    companyNatureMapper.delete(lambdaQueryWrapper);
                                }
                                companyNatureMapper.insert(object);

                                logger.debug(object.toString());

                            } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZZ_COUNTRY_AREA)){
                                CountryArea object = null;
                                object = new CountryArea();
                                object.setProvinceName(proviceName);
                                object.setYearMonthValue(yearMonthValue);
                                object.setFirstClass(first_class);
                                object.setSecondClass(second_class);

                                object.setGuid(UUID.randomUUID().toString());
                                object.setSysDate(new Date());
                                object.setIeType(ieType);

                                if(StringUtils.hasLength(value_1)){
                                    try{
                                        object.setTotalValue(new BigDecimal(value_1));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(StringUtils.hasLength(value_2)){
                                    try{
                                        object.setTotalValuePercent(new BigDecimal(value_2));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(!StringUtils.hasLength(value_1)&&!StringUtils.hasLength(value_2)&&!StringUtils.hasLength(value_3)&&!StringUtils.hasLength(value_3)){
                                    continue;
                                }

                                LambdaQueryWrapper<CountryArea> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                                lambdaQueryWrapper.eq(CountryArea::getYearMonthValue,object.getYearMonthValue());
                                lambdaQueryWrapper.eq(CountryArea::getProvinceName,object.getProvinceName());
                                lambdaQueryWrapper.eq(CountryArea::getFirstClass,object.getFirstClass());
                                lambdaQueryWrapper.eq(CountryArea::getSecondClass,object.getSecondClass());
                                lambdaQueryWrapper.eq(CountryArea::getIeType,object.getIeType());

                                CountryArea dbobject = countryAreaMapper.selectOne(lambdaQueryWrapper);
                                if(null != dbobject){
                                    countryAreaMapper.delete(lambdaQueryWrapper);
                                }
                                countryAreaMapper.insert(object);

                                logger.debug(object.toString());

                            } else if(dataType.equals(DataTableConstant.DATA_TYPE_ZZ_TRADE_MODE)){
                                TradeMode object = null;
                                object = new TradeMode();
                                object.setProvinceName(proviceName);
                                object.setYearMonthValue(yearMonthValue);
                                object.setFirstClass(first_class);
                                object.setSecondClass(second_class);

                                object.setGuid(UUID.randomUUID().toString());
                                object.setSysDate(new Date());
                                object.setIeType(ieType);

                                if(StringUtils.hasLength(value_1)){
                                    try{
                                        object.setTotalValue(new BigDecimal(value_1));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(StringUtils.hasLength(value_2)){
                                    try{
                                        object.setTotalValuePercent(new BigDecimal(value_2));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(!StringUtils.hasLength(value_1)&&!StringUtils.hasLength(value_2)&&!StringUtils.hasLength(value_3)&&!StringUtils.hasLength(value_3)){
                                    continue;
                                }

                                LambdaQueryWrapper<TradeMode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                                lambdaQueryWrapper.eq(TradeMode::getYearMonthValue,object.getYearMonthValue());
                                lambdaQueryWrapper.eq(TradeMode::getProvinceName,object.getProvinceName());
                                lambdaQueryWrapper.eq(TradeMode::getFirstClass,object.getFirstClass());
                                lambdaQueryWrapper.eq(TradeMode::getSecondClass,object.getSecondClass());
                                lambdaQueryWrapper.eq(TradeMode::getIeType,object.getIeType());

                                TradeMode dbobject = tradeModeMapper.selectOne(lambdaQueryWrapper);
                                if(null != dbobject){
                                    tradeModeMapper.delete(lambdaQueryWrapper);
                                }
                                tradeModeMapper.insert(object);
                                logger.debug(object.toString());
                            } else {
                                if(!StringUtils.hasLength(first_class)){
                                    continue;
                                }
                                commonDomain = new CommonDomain();
                                commonDomain.setSysDate(new Date());
                                commonDomain.setGuid(UUID.randomUUID().toString());
                                commonDomain.setDataSrc(dataSrc);
                                commonDomain.setFirstClass(first_class);
                                commonDomain.setSecondClass(second_class);
                                commonDomain.setThirdClass(third_class);
                                commonDomain.setFourthClass(fourth_class);
                                commonDomain.setYearMonthValue(yearMonthValue);
                                commonDomain.setProvinceName(proviceName);
                                commonDomain.setIeType(ieType);
                                commonDomain.setDataType(dataType);
                                commonDomain.setNote(StringUtils.hasLength(note)?note:null);
                                commonDomain.setUnit(StringUtils.hasLength(unit)?unit:null);
                                commonDomain.setNature(StringUtils.hasLength(nature)?nature:null);
                                commonDomain.setNote1(StringUtils.hasLength(note_1)?note_1:null);
                                commonDomain.setNote2(StringUtils.hasLength(note_2)?note_2:null);
                                if(StringUtils.hasLength(value_3)){
                                    try{
                                        commonDomain.setQty(new BigDecimal(value_3));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(StringUtils.hasLength(value_4)){
                                    try{
                                        commonDomain.setQtyPercent(new BigDecimal(value_4));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(StringUtils.hasLength(value_1)){
                                    try{
                                        commonDomain.setTotalValue(new BigDecimal(value_1));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(StringUtils.hasLength(value_2)){
                                    try{
                                        commonDomain.setTotalValuePercent(new BigDecimal(value_2));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                }
                                if(!StringUtils.hasLength(value_1)&&!StringUtils.hasLength(value_2)&&!StringUtils.hasLength(value_3)&&!StringUtils.hasLength(value_3)){
                                    continue;
                                }
                                logger.debug(commonDomain.toString());
                                LambdaQueryWrapper<CommonDomain> queryWrapper = new LambdaQueryWrapper();
                                queryWrapper.eq(StringUtils.hasLength(dataType),CommonDomain::getDataType,dataType);
                                queryWrapper.eq(StringUtils.hasLength(ieType),CommonDomain::getIeType,ieType);
                                queryWrapper.eq(StringUtils.hasLength(dataSrc),CommonDomain::getDataSrc,dataSrc);
                                queryWrapper.eq(StringUtils.hasLength(yearMonthValue),CommonDomain::getYearMonthValue,yearMonthValue);
                                queryWrapper.eq(StringUtils.hasLength(nature),CommonDomain::getNature,nature);
                                queryWrapper.eq(StringUtils.hasLength(fourth_class),CommonDomain::getFourthClass,fourth_class);
                                queryWrapper.eq(StringUtils.hasLength(third_class),CommonDomain::getThirdClass,third_class);
                                queryWrapper.eq(StringUtils.hasLength(second_class),CommonDomain::getSecondClass,second_class);
                                queryWrapper.eq(StringUtils.hasLength(first_class),CommonDomain::getFirstClass,first_class);
//                                if(StringUtils.hasLength(fourth_class)){
//                                    queryWrapper.eq(StringUtils.hasLength(fourth_class),CommonDomain::getFourthClass,fourth_class);
//                                } else {
//                                    if(StringUtils.hasLength(third_class)){
//                                        queryWrapper.eq(StringUtils.hasLength(third_class),CommonDomain::getThirdClass,third_class);
//                                    } else {
//                                        if(StringUtils.hasLength(second_class)){
//                                            queryWrapper.eq(StringUtils.hasLength(second_class),CommonDomain::getSecondClass,second_class);
//                                        } else {
//                                            queryWrapper.eq(StringUtils.hasLength(first_class),CommonDomain::getFirstClass,first_class);
//                                        }
//                                    }
//                                }
                                CommonDomain dbobject = commonDomainMapper.selectOne(queryWrapper);
                                if(null != dbobject){
                                    commonDomainMapper.delete(queryWrapper);
                                }
                                commonDomainMapper.insert(commonDomain);
//                            System.out.println(commonDomain.toString());
//                                System.out.println(first_class+","+second_class+","+third_class+","+fourth_class+","+value_2+","+value_1);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(),e);
        }
        return false;
    }

    public String getYearMonthFromTitle(String title) {
        String year, month;
        String temp = title;
        boolean getYearSuccess;
        do {
            if (temp.contains("年度") || temp.contains("月度")) {
                temp = temp.replace("年度", "").replace("月度", "");
                return null;
            }
            year = temp.substring(temp.indexOf("年") - 4, temp.indexOf("年"));
            month = temp.substring(temp.indexOf("年") + 1, temp.indexOf("月"));
            if (month.contains("至")) {
                month = month.substring(month.indexOf("至") + 1);
            }
            try {
                Integer.valueOf(year);
                getYearSuccess = true;
            } catch (Exception e) {
                temp = temp.substring(temp.indexOf("年") + 1);
                getYearSuccess = false;
            }
        } while (!getYearSuccess);

        return String.format("%4d%02d", Integer.valueOf(year), Integer.valueOf(month));
    }
}
